<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./css/main.css">
    <script src="./js/vue.js"></script>
    <script src="./js/axios.min.js"></script>
</head>
<style>
    input {
        border: none;
        width: 80%;
        height: 100%;
        outline: none;
    }

    .titleContianer {
        height: 50px;
        /* min-height: 50px; */
        /* word-wrap: break-word;
        word-break: normal; */
        /* font-size: 26px; */
    }

    .contianer {
        position: relative;
        border-bottom: 1px solid #f4f4f4;
        padding: 10px;
        background-color: white;
    }

    .other {
        font-size: 14px;

    }

    .l {
        z-index: 9;
    }

    .o {
        z-index: 10;
    }

    .otherInput {
        border: 1px solid #ccc;
        width: 60px;
        margin-left: 6px;
    }

    .reduce {
        position: absolute;
        right: 30px;
    }

    .funItem {
        font-size: 12px;
        margin-right: 20px;
        color: #1a73e8;
    }

    .contianerTitle {
        margin: 16px 16px 10px 16px;
        font-size: 10px;
        color: #ccc;
    }

    /* Switch开关样式 */
    input[type='checkbox'].switch {
        outline: none;
        appearance: none;
        -webkit-appearance: none;
        -moz-appearance: none;
        position: relative;
        width: 40px;
        height: 20px;
        background: #ccc;
        border-radius: 10px;
        transition: border-color .3s, background-color .3s;
        position: absolute;
        right: 0;
        margin-right: 16px;
    }

    input[type='checkbox'].switch::after {
        content: '';
        display: inline-block;
        width: 1rem;
        height: 1rem;
        border-radius: 50%;
        background: #fff;
        box-shadow: 0, 0, 2px, #999;
        transition: .4s;
        top: 2px;
        position: absolute;
        left: 2px;
    }

    input[type='checkbox'].switch:checked {
        background: rgb(19, 206, 102);
    }

    /* 当input[type=checkbox]被选中时：伪元素显示下面样式 位置发生变化 */
    input[type='checkbox'].switch:checked::after {
        content: '';
        position: absolute;
        left: 55%;
        top: 2px;
    }

    .openTypeContainer {
        animation: openTypeContainer .5s;
        animation-fill-mode: forwards;
    }

    /*自定义动画*/
    @keyframes openTypeContainer {
        0% {
            transform: translateY(-42.8px);
        }

        100% {
            transform: translateY(0);
        }
    }

    .closeTypeContainer {
        animation: closeTypeContainer .5s;
        animation-fill-mode: forwards;
    }

    /*自定义动画*/
    @keyframes closeTypeContainer {
        0% {
            transform: translateY(0);
        }

        100% {
            transform: translateY(-42.8px);
        }

    }

    .hint {
        width: 100vw;
        height: 18px;
        background-color: salmon;
        padding: 4px;
        color: white;
        z-index: 99;
        position: absolute;
        top: -28px;
        /* top: 0px; */
        box-sizing: content-box;
        font-size: 12px;
        line-height: 18px;
    }


    .openHint {

        animation: openHint .5s;
        animation-fill-mode: forwards;
    }

    /*自定义动画*/
    @keyframes openHint {
        0% {
            transform: translateY(0px);
        }

        100% {
            transform: translateY(26px);
        }
    }

    .closeHint {

        animation: closeHint .5s;
        animation-fill-mode: forwards;
    }

    /*自定义动画*/
    @keyframes closeHint {
        0% {
            transform: translateY(26px);
        }

        100% {
            transform: translateY(0);
        }
    }
</style>

<body>
    <div id="app">
        <div :class="[isShowHint!=-1?isShowHint==1?'hint openHint':'hint closeHint':'hint']">{{hintMsg}}</div>
        <!-- <div class="hint closeHint">{{hintMsg}}</div> -->
        <div class="contianer titleContianer l">
            <input type="text" :placeholder="titlePlaceHolder" v-model="itemTitle">

        </div>
        <div v-show="globalType!='填空'" :class="[redu<index&&redu!=-1?'contianer closeTypeContainer':'contianer']"
            v-for="(item,index) in item">
            <div v-if="item.type=='normal'">
                <input type="text" :placeholder="itemPlaceHolder" v-model="item.msg">
                <span class="reduce" @click="reduce(index)">--</span>
            </div>
            <div v-if="item.type=='other'">
                <span class="other">其他</span>
                <input type="text" class="otherInput">
                <span class="reduce" @click="reduce(index)">--</span>
            </div>

        </div>



        <div :class="[redu!=-1?'closeTypeContainer':add==1?'openTypeContainer':'']">
            <div class='contianer' v-show="globalType!='填空'">
                <span class="funItem" @click="addItem">+ 选项</span>
                <span class="funItem" @click="addOther">+ 其他</span>
            </div>
            <p class="contianerTitle">题目设置</p>
            <div class="contianer">
                <span class="other">此题必答</span>
                <input class="switch" type="checkbox" :checked="isMust">
            </div>
        </div>
        <mydialog v-show="isShowDialog" title="确定要删除该选项？" @cancel="dialogCancel" @confirm="dialogConfirm"></mydialog>
        <mydialog v-show="isShowGlobelDialog" title="确定放弃对此题的编辑？" @cancel="globalCancel" @confirm="globalConfirm">
        </mydialog>
    </div>
</body>
<script src="./js/main.js"></script>
<script>

    const app = new Vue({
        el: '#app',
        data: {
            globalType: '单选',
            hintMsg: "请编辑题目名称",
            titlePlaceHolder: "点击编辑题目名称",
            itemPlaceHolder: "点击编辑选项",
            itemTitle: "",
            item: [
                {
                    type: "normal",
                    msg: ""
                },
                {
                    type: "normal",
                    msg: ""
                },
            ],
            add: 0,
            redu: -1,
            isShowHint: -1,
            isMust: true,
            isShowDialog: false,
            isShowGlobelDialog: false,
        },
        created: function () {
            // this.save()
        },
        methods: {
            setType: function (type) {
                this.globalType = type
            },
            save: function () {
                let flag = true
                if (this.itemTitle === "") {
                    this.hintMsg = "点击编辑题目名称"
                    this.isShowHint = 1
                    setTimeout(() => {
                        this.isShowHint = 2
                    }, 3000)
                    flag = false
                }
                this.item.forEach(element => {
                    if (element.type == "normal" && (element.msg === "" || element.msg === undefined) && this.globalType != '填空') {
                        this.hintMsg = "请编辑选项"
                        this.isShowHint = 1
                        setTimeout(() => {
                            this.isShowHint = 2
                        }, 3000)
                        flag = false
                    }
                });

                if (flag) {
                    return {
                        type: this.globalType,
                        itemTitle: this.itemTitle,
                        items: this.globalType === '填空' ? [{
                            type: "",
                            msg: ""
                        }] : this.item,
                        isMust: this.isMust
                    }
                }

            },
            showGlobalDialog: function () {
                this.isShowGlobelDialog = true
            },
            globalCancel: function () {
                this.isShowGlobelDialog = false
            },
            globalConfirm: function () {
                this.isShowGlobelDialog = false
                // // 退出activity
                EditQuesItemJSFace.finish()
            },
            dialogCancel: function () {
                this.isShowDialog = false
            },
            dialogConfirm: function () {
                this.redu = this.delIndex
                this.isShowDialog = false
                setTimeout(() => {
                    this.redu = -1
                    this.item.splice(this.delIndex, 1)

                }, 500)
            },
            reduce: function (index) {
                // bug 值被清空
                if (this.item[index].msg !== "" && this.item[index].msg !== undefined) {
                    this.isShowDialog = true
                    this.delIndex = index
                    return
                }
                this.redu = index
                setTimeout(() => {
                    this.redu = -1
                    this.item.splice(index, 1)

                }, 500)
            },
            addItem: function () {
                this.item.push({
                    type: "normal",
                })

                this.beginTopToBottomAnima()
            },
            addOther: function () {
                this.item.push({
                    type: "other",
                })

                this.beginTopToBottomAnima()
            },
            beginTopToBottomAnima: function () {
                this.add = 1
                setTimeout(() => {
                    this.add = 2
                }, 500)
            },
            beginBottomToTopAnima: function () {
                this.redu = 1
                setTimeout(() => {
                    this.redu = 2
                }, 500)
            },

        },

    })




</script>

</html>